/* ==================================================================   
 * Created 2013-10-25下午3:12:22 by 郑伟
 * ==================================================================  
 *  EMV  Level2
 * Copyright (c) SZZCS S&T Co.ltd ShenZhen, 2012-2013 
 * ================================================================== 
 * 深圳市中磁计算机技术有限公司拥有该文件的使用、复制、修改和分发的许可权
 * 如果你想得到更多信息，请访问 <http://www.szzcs.com/cn>
 *
 * SZZCS S&T Co.ltd ShenZhen owns permission to use, copy, modify and 
 * distribute this documentation.
 * For more information on EMV, please 
 * see <http://www.szzcs.com/cn>.  
 * ================================================================== 
 */
package com.hangup.activity;

import com.devil.bbsclient.R;
import com.hangup.net.ActivityKeys;
import com.hangup.net.HttpUtil;
import com.hangup.util.NetWorkUtil;
import com.hangup.util.PublicUtil;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 帐号注册
 * 
 */
public class RegisterActivity extends LoginActivity {
	private Button btRegister;
	private TextView tv;
	private View loginView;
	private View registerView;
	private Dialog dialog;

	private EditText etName;
	private EditText etPass;
	private EditText etNick;
	private String name_;
	private String pass_;
	private String nick_;
	private String url;
	private String result;

	Handler handler_ = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 0:
				dialog.cancel();
				Toast.makeText(RegisterActivity.this,
						R.string.register_failure, Toast.LENGTH_LONG).show();
				break;
			case 1:
				// 成功...
				etName.setText("");
				etPass.setText("");
				etNick.setText("");
				Intent intent = new Intent();
				intent.setClass(RegisterActivity.this, LoginActivity.class);
				dialog.cancel();
				startActivity(intent);
				break;
			case 2:
				Toast.makeText(RegisterActivity.this, R.string.net,
						Toast.LENGTH_SHORT).show();
				break;
			case 3:
				dialog = NetWorkUtil.showDialog(RegisterActivity.this, null,
						"注册", "正在注册,请稍等");
				dialog.show();
				break;
			case 4:
				dialog.cancel();
				Toast.makeText(RegisterActivity.this, "连接服务器异常!",
						Toast.LENGTH_LONG).show();
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		btRegister = (Button) getWindow().findViewById(R.id.btRegister);
		btRegister.setVisibility(View.GONE);
		tv = (TextView) findViewById(R.id.titleLoginText);
		tv.setText(R.string.register);
		loginView = findViewById(R.id.login_div);
		loginView.setVisibility(View.GONE);
		loginView = findViewById(R.id.register_div);
		loginView.setVisibility(View.GONE);

		RelativeLayout rl = (RelativeLayout) findViewById(R.id.login_main);
		LayoutInflater inflater = getLayoutInflater();
		registerView = inflater.inflate(R.layout.activity_register, null);
		rl.addView(registerView);

		Button rgt = (Button) findViewById(R.id.register_button);
		rgt.setOnClickListener(new rgtOnclick());

	}

	private final class rgtOnclick implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			etName = (EditText) findViewById(R.id.register_username_edit);
			etPass = (EditText) findViewById(R.id.register_password_edit);
			etNick = (EditText) findViewById(R.id.register_nick_edit);
			name_ = etName.getText().toString();
			pass_ = etPass.getText().toString();
			nick_ = etNick.getText().toString();
			if (name_ == null || name_.equals("")) {
				PublicUtil.setFocus(etName);
				etName.setError("用户名不能为空!");
				return;
			} else if (pass_ == null || pass_.equals("")) {
				PublicUtil.setFocus(etPass);
				etPass.setError("密码不能为空!");
				return;
			} else if (nick_ == null || nick_.equals("")) {
				PublicUtil.setFocus(etNick);
				etNick.setError("昵称不能为空!");
				return;
			} else {
				if (NetWorkUtil.NetType(RegisterActivity.this) == null) {
					handler_.sendMessage(handler_.obtainMessage(2));
					return;
				}
				handler_.sendMessage(handler_.obtainMessage(3));
				register();
			}
		}
	}

	// 注册线程
	protected void register() {
		url = ActivityKeys.BASE_URL + ActivityKeys.REGISTER_URL + "name="
				+ name_ + "&pass=" + pass_ + "&nick=" + nick_;
		new Thread(new Runnable() {
			@Override
			public void run() {
				if (!NetWorkUtil.webServer()) {
					handler_.sendMessage(handler_.obtainMessage(4));
					return;
				}
				result = HttpUtil.getHttpPostResultForUrl(url);
				if (result == null) {// 注册失败
					handler_.sendMessage(handler_.obtainMessage(0));
					return;
				} else if (result.equals(ActivityKeys.NET_FAILURE)) {
					handler_.sendMessage(handler_.obtainMessage(0));
				} else {
					handler_.sendMessage(handler_.obtainMessage(1));
				}
			}
		}).start();
	}
}
